package com.wxxymaker.edu.service;

import com.blade.ioc.annotation.Bean;
import com.wxxymaker.edu.model.entity.Course;
import com.wxxymaker.edu.model.entity.CourseCollect;
import com.wxxymaker.edu.utils.TimeUtils;
import io.github.biezhi.anima.Anima;
import io.github.biezhi.anima.core.Joins;
import io.github.biezhi.anima.enums.OrderBy;
import lombok.extern.slf4j.Slf4j;
import io.github.biezhi.anima.page.Page;


/**
 * @Author: zhanghuan
 * @Date: 2018/9/14 下午8:30
 * @DESCRIBE: 课程收藏业务层
 */

@Bean
@Slf4j
public class CourseCollectService implements Service<CourseCollect> {


    @Override
    public CourseCollect add(CourseCollect courseCollect) {
        courseCollect.setTime(TimeUtils.getTime());
        courseCollect.setExist(1);
        boolean rollback = Anima.atomic(() -> {
            Integer asInt = courseCollect.save().asInt();
            courseCollect.setId(asInt);
        }).catchException(e -> {
            log.error("添加收藏失败！{}", e.getMessage());
        }).isRollback();
        return rollback ? null : courseCollect;
    }


    @Override
    public CourseCollect delete(Object pk) {
        CourseCollect courseCollect = byId(pk);
        boolean rollback = Anima.atomic(courseCollect::delete).catchException(e -> {
            log.error("删除失败！{}", e.getMessage());
        }).isRollback();
        return rollback ? null : courseCollect;
    }

    /**
     * 取消收藏
     *
     * @param uid 用户id
     * @param kid 课程id
     * @return
     */
    public boolean cancel(int uid, int kid) {
        boolean rollback = Anima.atomic(() -> {
            Anima.delete().from(CourseCollect.class)
                    .where(CourseCollect::getUserId, uid)
                    .where(CourseCollect::getCourseId, kid)
                    .execute();
        }).catchException(e -> {
            log.error("取消收藏失败！{}", e.getMessage());
        }).isRollback();
        return !rollback;
    }

    /**
     * 通过课程id取消该课程下的所有加入收藏 取消课程发布时使用
     * @param kid 课程id
     */
    public boolean cancels(int kid) {
        return Anima.update().from(CourseCollect.class)
                .set(CourseCollect::getExist, 0)
                .where(CourseCollect::getCourseId, kid)
                .execute() > 0;
    }


    /**
     * 检测该课程是不是被加入收藏
     * @param kid 课程id
     * @param uid 用户id
     */
    public boolean isCollect(int uid, int kid) {
        return Anima.select().from(CourseCollect.class)
                .where(CourseCollect::getUserId, uid)
                .where(CourseCollect::getCourseId, kid)
                .count() > 0;
    }

    /**
     * 获取用户收藏课程信息
     * 带上对应课程
     *
     * @param uid  用户id
     * @param page 页码
     * @param size 分页尺寸
     * @return
     */
    public Page<CourseCollect> page(int uid, int page, int size) {
        return Anima.select().from(CourseCollect.class)
                .where(CourseCollect::getUserId, uid)
                .where(CourseCollect::getExist, 1)
                .join(
                        Joins.with(Course.class)
                                .as(CourseCollect::getCourse)
                                .on(CourseCollect::getCourseId, Course::getId)
                                .order(CourseCollect::getId, OrderBy.ASC))
                .page(page, size);

    }


    @Override
    public boolean change(CourseCollect courseCollect) {
        return false;
    }

    @Override
    public CourseCollect byId(Object pk) {
        return null;
    }


}
